<?
class cuenta_bancaria{

	// Propiedades
	var $id;
	var $id_zona;
	var $id_banco;
	var $tipo;	
	var $numero;
	var $nombre_banco;
	var $total;

	function get($conn, $id){
		try {
			$q = "SELECT A.*, B.nombre AS nombre_banco FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id WHERE A.id=$id";
			//die($q);
			$r = $conn->Execute(utf8_encode($q));
			if(!$r->EOF){
				$this->id = $r->fields['id'];
				$this->id_zona = $r->fields['id_zona'];
				$this->id_banco = $r->fields['id_banco'];
				$this->nombre_banco = $r->fields['nombre_banco'];
				$this->tipo = $r->fields['tipo'];
				$this->numero = $r->fields['numero'];
			}
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function get_all($conn, $orden="B.nombre, A.numero", $Tipo, $Valor, $TipoE, $max=10, $from=1){
		try {
			$id_zona = $_SESSION['SesionZona'];
			$TipoE = empty($TipoE) ? 0 : $TipoE;
			
			if(empty($Valor)){
				$q = "SELECT A.id FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id ";
				$q.= "WHERE A.id_zona = $id_zona ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==0){
				$q = "SELECT A.id FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id ";
				$q.= "WHERE A.id_zona = $id_zona  AND A.id::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			//	die($q);			

			}
			elseif($Tipo==1){
				$q = "SELECT A.id FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id ";
				$q.= "WHERE A.id_zona = $id_zona  AND B.nombre::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==2){
				$q = "SELECT A.id FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id ";
				$q.= "WHERE A.id_zona = $id_zona  AND A.numero::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			//die($q);			
			$r = ($max!=0) ? $conn->SelectLimit($q, $max, $from) : $conn->Execute(utf8_encode($q));
			$coleccion=array();
			while(!$r->EOF){
				$ue = new cuenta_bancaria;
				$ue->get($conn, $r->fields['id']);
				$coleccion[] = $ue;
				$r->movenext();
			}
			$this->total = $r->RecordCount();
			return $coleccion;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function add($conn, $id_banco, $tipo, $numero){
		try {
			$id_zona = $_SESSION['SesionZona'];
			$q = "INSERT INTO cuentas_bancarias ";
			$q.= "(id_zona, id_banco, tipo, numero) ";
			$q.= "VALUES ";
			$q.= "($id_zona, $id_banco, upper('$tipo'), upper('$numero')) ";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_ADD_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function set($conn, $id, $id_banco, $tipo, $numero){
		try {
			$q = "UPDATE cuentas_bancarias SET id_banco=$id_banco, tipo=upper('$tipo'), numero=upper('$numero') ";
			$q.= "WHERE id=$id";	
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_SET_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function del($conn, $id){
		try {
			$q = "DELETE FROM cuentas_bancarias WHERE id=$id";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_DEL_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}
	
	function total_registro_busqueda($conn,$Tipo, $Valor, $TipoE, $orden="A.id")
	{
		$id_zona = $_SESSION['SesionZona'];
		$TipoE = empty($TipoE) ? 0 : $TipoE;
		if(empty($Valor)){
			$q = "SELECT count(*) AS total FROM cuentas_bancarias AS A WHERE A.id_zona = $id_zona";
		}
		elseif($Tipo==0){
			$q = "SELECT count(*) AS total FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id";
			$q.= " WHERE A.id_zona = $id_zona  AND id::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==1){
			$q = "SELECT count(*) AS total FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id";
			$q.= " WHERE A.id_zona = $id_zona  AND B.nombre::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==2){
			$q = "SELECT count(*) AS total FROM cuentas_bancarias AS A INNER JOIN bancos AS B ON A.id_banco = B.id";
			$q.= " WHERE A.id_zona = $id_zona  AND A.numero::text ILIKE '%$Valor%'::text";
		}
		$r = $conn->Execute(utf8_encode($q));
		$total = $r->fields['total'];
		return $total;
	}

	function auditoria($conn, $query) {
		$id_usuario = $_SESSION['id'];
		$q = "INSERT INTO auditoria(id_usuario, query) VALUES($id_usuario,$$" . $query . "$$);";
		//die($q);
		$conn->Execute(utf8_encode($q));

		return true;
	}			

}
?>
